package com.originskyseed.platform.cms.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.cms.i18n.CmsResponseMsg;
import com.originskyseed.platform.cms.model.CmsRelateView;
import com.originskyseed.platform.cms.service.ICmsRelateViewService;
import com.originskyseed.platform.cms.vo.request.CmsRelateViewRequestVO;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;

/**
 * 新闻关联查看人员信息表Controller类（OSI_CMS_RELATE_VIEW）
 *
 * @author wlj
 */
@Controller
@RequestMapping("/cmsRelateView")
@BaseLocaleResource(localeFileName = "i18n_cms")
public class CmsRelateViewController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CmsRelateViewController.class);

	@DubboReference
	private ICmsRelateViewService cmsRelateViewService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cmsRelateViewList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<CmsRelateView>> cmsRelateViewList(HttpServletRequest request) {
		BaseResponseBody<BasePage<CmsRelateView>> result = null;
		try {
			// 请求对象
			CmsRelateViewRequestVO requestVO = this.getParameterBean(CmsRelateViewRequestVO.class);

			// 分页查询
			BasePage<CmsRelateView> page = new BasePage<CmsRelateView>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.cmsRelateViewService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<CmsRelateView>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<CmsRelateView>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cmsRelateViewSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<CmsRelateView> cmsRelateViewSave(HttpServletRequest request) {
		BaseResponseBody<CmsRelateView> result = null;
		try {
			// 请求对象
			CmsRelateView cmsRelateView = this.getParameterBean(CmsRelateView.class);

			if (StringUtils.isNotBlank(cmsRelateView.getId())) {
				this.cmsRelateViewService.updateByPrimaryKey(cmsRelateView);
			} else {
				this.cmsRelateViewService.insertSelective(cmsRelateView);
			}
			result = new BaseResponseBody<CmsRelateView>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_CONTENT_SAVE_SUCCESS.getMsg()), cmsRelateView);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<CmsRelateView>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_CONTENT_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cmsRelateViewDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> cmsRelateViewDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.cmsRelateViewService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.UPDATE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.UPDATE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
